/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui.report;

import easybooking.ReportClientController;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

/**
 *
 * @author Tim Schröder
 */
public class RCFlightSeatingReportDialog extends JDialog {

    private ReportClientController rcc;
    private JPanel dialogPanel;
    private JPanel buttonPanel;
    private JButton btnClose;
    private JScrollPane sp;
    private JEditorPane edpReport;
    private String report;
    private JButton btnSaveReport;

    /**
     * constructor for the RCFlightSeatingReportDialog
     * @param rcif, the ReportClientController controller
     */
    public RCFlightSeatingReportDialog(ReportClientController rcif) {
        rcc = rcif;
        //generateComponents();
    }

    /**
     * Get Text Fields
     * @return the value of the textfields
     */
    public String getReport() {
        return report;
    }

    /**
     * creates and shows the dialog
     * @param report, the report for which the dialog should be created and
     * showed
     */
    public void createAndShowDialog(String report) {
        this.report = report;
        generateComponents();
        this.setResizable(true);
        this.setTitle("Flight Seating Report");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    /**
     * calls the generateComponents() method to create and show the FlightSeatingReportDialog
     */
    public void createAndShowDialog() {
        generateComponents();
        btnClose.setName("createFlight");
        this.setResizable(true);
        this.setTitle("Create Flight");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    // Preperes the dialog
    /**
     * Generates all components to create an display the ReportGUI
     */
    private void generateComponents() {

        // Dialog Settings

        dialogPanel = new JPanel();


        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }
        dialogPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();


        edpReport = new JEditorPane("text/html", report);
        edpReport.setEditable(false);
        addComponent(dialogPanel, gbl, this.edpReport, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0, 0, 2, 1, 0, 0);

        // adds to buttonPanel
        addComponent(dialogPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 6, 2, 1, 0, 0);

        // sets die dialogPanel as contentPane
        sp = new JScrollPane(dialogPanel);
        this.setContentPane(sp);


    }

    // Creates the button panel
    /**
     * creates a a button to create a flight seating report
     * @return a JPanal with two buttons
     */
    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();

        btnSaveReport = new JButton("Save Report");
        btnSaveReport.setActionCommand("SaveFlightSeatingReport");
        btnSaveReport.addActionListener(rcc);


        btnClose = new JButton("Close");
        btnClose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // Adds the buttons to the buttonPanel
        buttonPanel.add(btnClose);
        buttonPanel.add(btnSaveReport);
        return buttonPanel;
    }

    /**
     * defines the layout of the GUI
     * @param cont container to which the component is added
     * @param gbl GridBagLayout object
     * @param c compontent which is added to the container
     * @param fill how to use free space in the cell around the component
     * @param anchor where to fix an align the component
     * @param x x position in the grid layout
     * @param y y position in the grid layout
     * @param width number of columns the element should spread
     * @param hight number of rows the element should spread
     * @param weightx handels the horizontal free space
     * @param weighty handels the vertical free space
     */
    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
